//	TestLexer.java

import java.io.*;
//import Interpreter.*;
/** 
	Description: Test program for the Lexer class

	@author Teresa Cole
	@version cs354s04
*/
public class TestLexer implements Token
{
    /**  Creates a Lexer whose input comes from a file named as the first
     * command line argument if there is one or from System.in if there isn't.

     <p>The program scans the input one Lexeme at a time and prints the token
     and any associated value for each Lexeme.

     <p>Use Ctrl/D to terminate input from the keyboard.
    */
    static public void main(String[] args)  throws IOException
    {
    System.out.println( "Simple Lexer ");

    Lexer scanner = null;

    if (args.length==0)
	{
	scanner = new Lexer( System.in);
	System.out.println( "Enter code; use CTRL-D to quit. ");
	}
    else
	try
	    {
	    scanner = new Lexer( new FileInputStream( args[0]));
	    }
	catch (IOException e)
	    {
	    System.err.println( "Lexer: Error opening file " 
		+ args[0] + "\n" + e);
	    System.exit( 1);
	    }

    Lexeme next = null;
    try
	{
	do
	    {
	    next = scanner.next();
	    next.print( System.out);
	    } while (next.getType() != EOF);
	}
	catch (Exception e)
	    {
	    if ( e instanceof EOFException)
		{
		System.out.println( "\nExiting ...\n\n");
		System.exit(1);
		}
	    else
		{
		System.err.println( "Lexer: Error reading lexeme " 
		    + "\n" + e);
		System.exit( 1);
		}
	    }

    
    System.out.println( "exiting ... ");
	    
    }
	
}
